<?php
namespace App\Services\SevenCattle;

use App\Models\ImFilesStorage;
use App\Services\Configs\DefaultService;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;

class UploadServices
{
    private $accessKey; //开发者平台分配的AppKey
    private $secretKey; //开发者平台分配的AppSecret,可刷新
    private $publicUrl; //公开访问域名
    private $privateUrl; //私有访问域名
    private $privateBucket; //私有桶
    private $publicBucket; //公开桶

    public function __construct()
    {
        $config = new DefaultService();
        $data = $config->first('qiqiu');
        $this->accessKey = $data['accessKey'];
        $this->secretKey = $data['secretKey'];
        $this->publicUrl = $data['publicUrl'];
        $this->privateUrl = $data['privateUrl'];
        $this->privateBucket = $data['privateBucket'];
        $this->publicBucket = $data['publicBucket'];
    }

    /**
     *七牛云上传（图片/视频）
     * @param $file  $request->file('file');
     * @param $folder 路径加文件名
     * @param string $power 私有1, 公共读2,公共读写3
     *author:tangfen
     *date:$ct
     */
    public function sevenUpload($file,$folder, $power=2)
    {
        // 构建鉴权对象
        $auth = new Auth($this->accessKey, $this->secretKey);
        // 要上传的空间
        if($power==1){//私有桶
            $bucket = $this->privateBucket;
        }else{//公开桶
            $bucket = $this->publicBucket;
        }
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        // 要上传文件的本地路径
        $filePath = $file->getRealPath();
        // 上传到七牛后保存的文件名
        $key = $folder.'/'.date('Ymd') . '/' . date('YmdHis') . uniqid() . '.'.$file->getClientOriginalExtension();
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($res, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return 1;//失败
        } else {
            if($power==1){//私有
                $res['key'] = $this->privateUrl.$res['key'];
            }else{
                $res['key'] = $this->publicUrl.$res['key'];
            }
            return $res;
        }
    }

    /**
     *获取私有文件的访问凭证 返回文件的url  有效时间内才可以访问
     * @param $baseUrl
     * @param int $expires 凭证过期时间
     *author:tangfen
     *date:$ct
     */
    public function downloadUrl($baseUrl,$expires = 3600){
        $auth = new Auth($this->accessKey, $this->secretKey);
        $url = $auth->privateDownloadUrl($baseUrl,$expires);
        return $url;
    }

    /**
     *删除文件
     * @param $file_id
     *author:tangfen
     *date:$ct
     */
    public function delFile($file){
        $fileInfo = explode('com/',$file);
        //数据入库
        $ImFilesStorage = new ImFilesStorage();
        $power =  $ImFilesStorage->where('link_url', $file)->value('power'); //私有1, 公共读2,公共读写3
        if($power!=1){
            $bucket = $this->publicBucket; //公开桶
        }else{
            $bucket = $this->privateBucket; //私有桶
        }
        try {
            $auth  = new Auth($this->accessKey, $this->secretKey);
            $qiniuManager = new BucketManager($auth);
            $qiniuManager->delete($bucket, $fileInfo[1]);
            //软删数据库状态
            $file = ImFilesStorage::where('link_url', $file)->first();
            $file->status = 3;  //3已删除
            $file->save();
            return 2;//成功
        } catch (\Exception $e) {
            return 1;//请求失败  echo($e);
        }
    }

}

